require('js/lib/require_patch').monkeypatch(this); // android needs this. Apparently there are different contexts

var ui = require('js/ui/common/ui');

exports.TextView = function(params) {
	globals.util.debugDump('TextView params', params);
	var note = params;
	
	var instance = ui.view({
		backgroundColor:'#fff'
	});

	var textArea = Ti.UI.createTextArea({
	    value:note.text||'',
	    height:Ti.Platform.displayCaps.platformHeight/2,
	    width:'80%',
	    top:30,
	    font:{fontSize:20,fontFamily:'Marker Felt', fontWeight:'bold'},
	    color:'#888',
	    textAlign:'left',
	    appearance:Titanium.UI.KEYBOARD_APPEARANCE_ALERT,   
	    borderWidth:2,
	    borderColor:'#bbb',
	    borderRadius:5
	});
	
	instance.add(textArea);
	
	var selector = ui.view({
		height:'50%',left:Ti.Platform.displayCaps.platformWidth*-1,
		width:Ti.Platform.displayCaps.platformWidth,
		top:'10%',
		backgroundImage: globals.util.urls()['bg_toolbar'],
		borderColor: 'black', borderWidth:5, borderRadius:10
	});
	var addImage = Ti.UI.createImageView({image:globals.util.urls()['slideOut'], right:-5, height:'10%', width:'10%'});
	var expanded = false;
	addImage.addEventListener('click',function(e){
		var animation = Titanium.UI.createAnimation();
		animation.duration = 500;

		if (expanded) {
			animation.left = Ti.Platform.displayCaps.platformWidth*-0.92;
			selector.animate(animation);
			addImage.image = globals.util.urls()['slideOut'];
			expanded = false;
		}
		else {
			animation.left = 0;
			selector.animate(animation);
			addImage.image = globals.util.urls()['slideIn'];
			expanded = true;
		}
	});
	selector.add(addImage);
	var picker = Titanium.UI.createPicker({selectionIndicator: true, left:'5%', height:'35%', width: '80%'});
	picker.addEventListener('change',function(e){
		textArea.value = (textArea.value||'') + e.selectedValue + '. ';
		addImage.fireEvent('click',{});
	});
	selector.add(picker);
	instance.add(selector);
	
	// Control Bar
	var controlLabels = {back: 'Home'};
	var controlFunctions = {back: function() { 
			var note = saveResults();
			instance.fireEvent('app:dismiss.note', {note: note}); 
		}};
		controlLabels.next = 'Next';
		controlFunctions.next = function() { 
			saveResults();
			var categories = globals.db.listNoteCategories('sequence > ? ORDER BY sequence',[note.sequence]);
			if (categories.length > 1)
				categories[0].nextText = 'Next';
			instance.fireEvent('app:next.note', categories.length > 0 ? {next: categories[0]} : {}); 
		}
	var controlView = ui.controlBar(controlLabels,controlFunctions,{});
	if (!note.nextText) {
		controlView.removeNextControl();
	}
	
	instance.add(controlView);
	
	instance.updateParams = function(newParms) {
		note = newParms;
		textArea.value = note.text;
		if (note.nextText) {
			controlView.addNextControl();
		}
		else {
			controlView.removeNextControl();			
		}

		// Is there a picker?
		var pickList = globals.db.listPicklistItems('type = ?',[newParms.title]);
		if (pickList.length > 0) {
			selector.left = Ti.Platform.displayCaps.platformWidth*-0.92;
			expanded = false;
			addImage.image = globals.util.urls()['slideOut'];
			for (var i=0,l=pickList.length; i<l; i++) {
				pickList[i].title = pickList[i].description;
			}
			var col	 = picker.columns[0];
			if (col) {
				for (var i=col.rowCount-1; i>-1; i--) {
					col.removeRow(col.rows[i]);
				}
				for (var i=0,l=pickList.length; i<l; i++) {
					col.addRow(Ti.UI.createPickerRow({title:pickList[i].title}));
				}
			}
			else
				picker.add(pickList);
		}
		else {
			selector.left = Ti.Platform.displayCaps.platformWidth*-1;
		}
	}
	
	return instance;
	
	// Inner function used to persist note
	function saveResults(noteType) {
		if (textArea.value && textArea.value !== '') {
			var PatientNote = require('js/model/PatientNote');
			var patNote = new PatientNote({
				patient_id: globals.patient.id, 
				provider_id: globals.provider.id, 
				appointment_id: globals.appointment.id, 
				note_type: note.categoryId,
				note_text: textArea.value
			});
			if (note.id) {
				patNote.id = note.id;
				globals.db.updatePatientNote(patNote);
			}
			else {
				globals.db.addPatientNote(patNote);
			}
		}
		
		return patNote;
	}
};
